
/**
 * @Author 12629
 * @Description：
 */
public class BinaryTree {

    static class TreeNode {
        public char val;
        //存储左孩子节点的引用
        public TreeNode left;
        //存储右孩子节点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }


    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    public int countNode = 0;

    /**
     * 求节点个数 遍历思路
     * @param root
     */
    public void nodeSize(TreeNode root) {
        if(root == null) {
            return;
        }
        countNode++;
        nodeSize(root.left);
        nodeSize(root.right);
    }

    /**
     * 求节点个数 子问题思路
     * @param root
     * @return
     */
    public int nodeSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int tmp = nodeSize2(root.left) + nodeSize2(root.right) + 1;
        return tmp;
    }

    /**
     * 求叶子节点个数
     * 子问题：左树的叶子 + 右树的叶子  = 整棵树的叶子节点   递推公式
     * 什么是叶子：既没有左子树 又 没有右子树 此时这个节点叫做叶子节点
     * @param root
     * @return
     */
    public int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }else if(root.left == null && root.right == null) {
            return 1;
        }else {
            return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
        }
    }

    public int leafNodeSize;

    /**
     * 遍历思路 求叶子节点个数
     * @param root
     */
    public void getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafNodeSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    /**
     * 第K层节点的个数
     * @param root
     * @param k
     * @return
     */
    public int getKLeveNodeSize(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLeveNodeSize(root.left,k-1) +
                getKLeveNodeSize(root.right,k-1);
    }

    /**
     * 求二叉树的高度
     * 时间复杂度：O(n)
     * 空间复杂度：O(logN)
     * @param root
     * @return
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return getHeight(root.left) > getHeight(root.right) ?
                getHeight(root.left)+1 : getHeight(root.right)+1;
    }


    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return leftH > rightH ? leftH+1 : rightH+1;
    }


    public int maxDepth2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftH = maxDepth2(root.left);
        int rightH = maxDepth2(root.right);
        return Math.max(leftH,rightH)+1;
    }
}